package uk.co.cocking.anttasks;

import java.io.Reader;
import java.util.List;

import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Node;

import org.jaxen.XPath;
import org.jaxen.xom.XOMXPath;

public class XomXPathEvaluator implements XPathEvaluator {



   @SuppressWarnings("unchecked")
   @Override
   public String[] evaluate(String xpath, Reader reader) throws XPathEvaluatorException {
      Builder builder = new Builder();
      try {
         Document document = builder.build(reader);
         XPath path = new XOMXPath(xpath);
         
         List<Node> nodes = path.selectNodes( document);

         String[] result = new String[nodes.size()];
         for (int i = 0; i < nodes.size(); i++) {
            result[i] = nodes.get(i).getValue();
         }
         return result;
      } catch (Exception e) {
         throw new XPathEvaluatorException(e);
      } 
   }


}
